package service;

import dao.BookingMapper;
import entity.Booking;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;

public class BookingService {
    SqlSession sqlSession = null;
    /**
     * 询所有数据，查询结果按接种日期升序排列
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public List<Booking> list(@Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize){
        List<Booking> list = new ArrayList<Booking>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(BookingMapper.class).list(pageIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    public int delete(@Param("id") int id){
        int result = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(BookingMapper.class).delete(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    /**
     * 添加预约
     * @param booking
     * @return
     */
    public int add(Booking booking){
        int result = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(BookingMapper.class).add(booking);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    /**
     * 查询记录数，根据身份证号码判断是否接种过或预约疫苗
     * @param cardId
     * @return
     */
    public int result(String cardId){
        int result = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(BookingMapper.class).result(cardId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    /**
     * 修改指定预约信息
     * @param id
     * @return
     */
    public int update(int id){
        int result = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(BookingMapper.class).update(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    public List<Booking> select(@Param("cardId") String cardId,@Param("status") int status){
        List<Booking> list = new ArrayList<Booking>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(BookingMapper.class).select(cardId,status);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }
}
